package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.ReceiveAndDestDto;
import com.igeekhome.dto.SectionDestDto;
import com.igeekhome.dto.SectionNetworkDto;
import com.igeekhome.mapper.DNetworkMapper;
import com.igeekhome.mapper.DSendingMaintenanceListMapper;
import com.igeekhome.mapper.DestNetworkMapper;
import com.igeekhome.mapper.SysStaffMapper;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.DSendingMaintenanceList;
import com.igeekhome.pojo.DSendingMaintenanceListExample;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.service.ISendingMainListService;
import com.igeekhome.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 立以宁
 *
 *
 */
@Service("sendingMainListService")
public class SendingMainListServiceImpl implements ISendingMainListService {

    @Autowired
    private DestNetworkMapper destNetworkMapper;

    @Autowired
    private DNetworkMapper networkMapper;

    @Autowired
    private SysStaffMapper sysStaffMapper;

    @Autowired
    private DSendingMaintenanceListMapper dSendingMaintenanceListMapper;


    @Override
    public PageInfo<ReceiveAndDestDto> selectByAll(Integer page, Integer limit, String sectionName) {
        List<ReceiveAndDestDto> receiveAndDestDtos = new ArrayList<>();
        DSendingMaintenanceListExample example = new DSendingMaintenanceListExample();
        DSendingMaintenanceListExample.Criteria criteria = example.createCriteria();

        //为空返回值为真
        if (!CommonUtil.isEmpty(sectionName)) {
            criteria.andRangeNameLike(sectionName);
        }
        //查询单表数据返回结果
        List<DSendingMaintenanceList> dSendingMaintenanceLists = dSendingMaintenanceListMapper.selectByExample(example);
        //进行填充数据，并用逗号拼接id和网点名
        if (dSendingMaintenanceLists!= null) {
            for (DSendingMaintenanceList item : dSendingMaintenanceLists) {
                //将对应的id转换成姓名,放入item中
                ReceiveAndDestDto data = getData(item);
                receiveAndDestDtos.add(data);
            }
        } else {
            //查询结果不存在
            return null;
        }
        //进行分页
        PageHelper.startPage(page, limit);
        PageInfo<ReceiveAndDestDto> pageInfo = new PageInfo<>(receiveAndDestDtos);
        return pageInfo;
    }



    @Override
    public ReceiveAndDestDto selectById(Integer sectionId) {
        DSendingMaintenanceListExample example = new DSendingMaintenanceListExample();
        DSendingMaintenanceListExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(sectionId);
        List<DSendingMaintenanceList> dSendingMaintenanceLists = dSendingMaintenanceListMapper.selectByExample(example);

        if (dSendingMaintenanceLists!= null&&dSendingMaintenanceLists.size()!=0) {
            DSendingMaintenanceList dSendingMaintenanceList = dSendingMaintenanceLists.get(0);
            ReceiveAndDestDto data = getData(dSendingMaintenanceList);
            return data;
            }

        return null;
    }


    @Override
    public void add(ReceiveAndDestDto section) {
        DSendingMaintenanceList dSendingMaintenanceList = section.getDSendingMaintenanceList();
        dSendingMaintenanceListMapper.insert(dSendingMaintenanceList);
        Integer sectionType = dSendingMaintenanceList.getSectionType();
        String[] ids = section.getStrId().split(",");
        Integer id = dSendingMaintenanceList.getId();
        System.out.println("获取到的值："+ ids);
        //如果是0，插入目的地区间关系表，1插入网点区间关系表
        if ( sectionType == 0) {
            for (String splitId:ids) {
                System.out.println("id:"+splitId);
                if (!CommonUtil.isEmpty(splitId) && !splitId.equals("001") ) {
                    destNetworkMapper.insertNetworkRelation(id,Integer.valueOf(splitId));
                }
            }
        } else if ( sectionType == 1) {
            for (String splitId:ids) {
                System.out.println("id:"+splitId);
                if (!CommonUtil.isEmpty(splitId) && !splitId.equals("001")) {
                    destNetworkMapper.insertDestRelation(id, Integer.valueOf(splitId));
                }
            }
        }

    }

    @Override
    public void update(ReceiveAndDestDto section) {
        DSendingMaintenanceList dSendingMaintenanceList = section.getDSendingMaintenanceList();
        dSendingMaintenanceListMapper.updateByPrimaryKey(dSendingMaintenanceList);
        Integer sectionType = dSendingMaintenanceList.getSectionType();
        String[] ids = section.getStrId().split(",");
        Integer id = dSendingMaintenanceList.getId();
        if ( sectionType == 0 && sectionType != null) {
            //关系表更新时，先删除后插入
            destNetworkMapper.deleteNetworkRelation(id);
            for (String splitId:ids) {
                if (!CommonUtil.isEmpty(splitId) && !splitId.equals("001")) {
                    destNetworkMapper.insertNetworkRelation(id, Integer.valueOf(splitId));
                }
            }
        } else if ( sectionType == 1 && sectionType != null) {
            destNetworkMapper.deleteDestRelation(id);
            for (String splitId:ids) {
                if (!CommonUtil.isEmpty(splitId) && !splitId.equals("001")) {
                    destNetworkMapper.insertDestRelation(id, Integer.valueOf(splitId));
                }
            }
        }
    }

    /**
     * 先删除关系表，后删除被关联表
     * @param id 区间编号
     */
    @Override
    public void delete(Integer id, Integer sectionType) {
        if (sectionType == 0 && sectionType != null) {
            destNetworkMapper.deleteNetworkRelation(id);
        } else if (sectionType == 1 && sectionType != null) {
            destNetworkMapper.deleteDestRelation(id);
        }

        dSendingMaintenanceListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<DSendingMaintenanceList> selectBySectionType(Integer sectionType) {
        DSendingMaintenanceListExample example = new DSendingMaintenanceListExample();
        example.createCriteria().andStatusEqualTo(1).andSectionTypeEqualTo(sectionType);
        return dSendingMaintenanceListMapper.selectByExample(example);

    }

    //封装要查询的数据
    public ReceiveAndDestDto getData(DSendingMaintenanceList item) {

        ReceiveAndDestDto receiveAndDestDto = new ReceiveAndDestDto();
        Integer servicingManId = item.getServicingManId();
        Integer servicingNetworkId = item.getServicingNetworkId();
        DNetwork dNetwork = networkMapper.selectByPrimaryKey(servicingNetworkId);
        SysStaff staff = sysStaffMapper.selectByPrimaryKey(servicingManId);

        receiveAndDestDto.setDSendingMaintenanceList(item);
        receiveAndDestDto.setNetworkName(dNetwork.getNetworkName());
        receiveAndDestDto.setStaffName(staff.getName());
        Integer sectionType = item.getSectionType();

        if (sectionType == 0) {
            Integer id = item.getId();
            List<SectionNetworkDto> listOne = destNetworkMapper.selectNetworkMap(id);
            //如果查询结果不为空,也就是设置了区间,可能没有返回list,所以是空指针，而不是长度为零
            if (listOne != null) {
                receiveAndDestDto.setSendNetwork(listOne);
                for (SectionNetworkDto listItem:listOne) {
                    if (receiveAndDestDto.getStr() == null) {
                        receiveAndDestDto.setStr( "," + listItem.getName());
                        receiveAndDestDto.setStrId("," + listItem.getNetwork_id());
                    } else {
                        receiveAndDestDto.setStr(receiveAndDestDto.getStr() + "," + listItem.getName() );
                        receiveAndDestDto.setStrId(receiveAndDestDto.getStrId() + "," + listItem.getNetwork_id());
                    }

                }
            }

        } else {
            Integer id = item.getId();
            List<SectionDestDto> list = destNetworkMapper.selectDestMap(id);
            if (list != null) {
                receiveAndDestDto.setReceiveDest(list);
                for (SectionDestDto listItem:list) {
                    if (receiveAndDestDto.getStr() == null) {
                        receiveAndDestDto.setStr("," + listItem.getName());
                        receiveAndDestDto.setStrId("," +listItem.getDest_id() );
                    } else {
                        receiveAndDestDto.setStrId(receiveAndDestDto.getStrId() + "," +listItem.getDest_id() );
                        receiveAndDestDto.setStr(receiveAndDestDto.getStr() + "," + listItem.getName());
                    }

                }
            }

        }

        return receiveAndDestDto;
    }


}
